var app = getApp();
const pathUrl = app.globalData.pathUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pathUrl: pathUrl,
        openId: '',
        queryResults: '',
        violationTimes: '',
        totalDeduction: '',
        totalFine: '',//总罚款
        list: ' ',
        ocrName: '', //ocr姓名
        ocrSex: '', //ocr性别
        ocrNation: '', //ocr民族
        ocrBirth: '', //ocr出生
        ocrAddress: '', //ocr地址
        ocrIdNumber: '', //ocr身份证号
        ocrOffice: '', //ocr签发机关
        ocrValidityPeriod: '', //ocr有效期限
        cusIdValidfrom: '', //证件开始日期
        cusIdValidto: '', //证件结束日期
        brand: '', //品牌/车型
        plateNumber: '', //车牌号
        holder: '', //所有人
        dateIssue: '', //注册/发证日期
        nature: '', //使用性质
        address: '', //住址
        vinno: '', //车架号
        engineNumber: '', //发动机号
        residentialAddress: '', //居住地址
        telephone: '', //联系电话
        checkedValue: '', //婚姻情况
        spouseName: '', //配偶姓名
        spouseTelephone: '', //联系电话
        urgent: '', //紧急联系人
        urgentTelephone: '', //紧急联系人联系电话
        urgent2: '', //紧急联系人
        urgentTelephone2: '', //紧急联系人联系电话
        isSubmit: true, //防止重复提交
        guaranteeName: '', //担保人姓名
        guaranteeIdCard: '', //担保人身份照
        guaranteePhone: '', //担保人电话
        orderAmount: '', //金额
        pickerIndex: '3',//选择期数index
        orderPeriod: '36',//期数
        orderBy6: '',
        orderBy7: '',
        orderBy8: '',
        orderBy4: '',
        orderBy5: '',



    },

    bindPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            index: e.detail.value
        })
    },
    //银行代码
    bankCodeInput:function(e){
        this.setData({
            bankCode:e.detail.value
        })
    },
    //银行卡号
    accountNoInput: function (e) {
        this.setData({
            accountNo: e.detail.value
        })
    },
    //电话
    mobileInput: function (e) {
        this.setData({
            mobile: e.detail.value
        })
    },
    //ocr名字赋值
    nameInput: function (e) {
        this.setData({
            name: e.detail.value
        })
    },
    //ocr身份证号
    IdCardInput: function (e) {
        this.setData({
            IdCard: e.detail.value
        })
    },
    orderBy6Input: function (e) {
        this.setData({
            orderBy6: e.detail.value
        })
    },
    //
    orderBy7Input: function (e) {
        this.setData({
            orderBy7: e.detail.value
        })
    },  //
    orderBy8Input: function (e) {
        this.setData({
            orderBy8: e.detail.value
        })
    },
    //储蓄卡
    orderBy9Input: function (e) {
        this.setData({
            orderBy9: e.detail.value
        })
    },
    //开户行
    orderBy10Input: function (e) {
        this.setData({
            orderBy10: e.detail.value
        })
    },
    //银行预留电话
    orderBy11Input: function (e) {
        this.setData({
            orderBy11: e.detail.value
        })
    },
    upEnclosure: function (e) {
        var that = this;
        var sbtype = e.currentTarget.dataset.sbtype
        wx.chooseImage({
            count: 1, //最多可以选择的图片总数  
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
                var tempFilePaths = res.tempFilePaths;
                //启动上传等待中...  
                wx.showToast({
                    title: '正在上传...',
                    icon: 'loading',
                    mask: true,
                    duration: 20000
                })
                var uploadImgCount = 0;
                for (var i = 0, h = tempFilePaths.length; i < h; i++) {
                    wx.uploadFile({
                        url: pathUrl + '/rest/base/uploadsb',
                        filePath: tempFilePaths[i],
                        name: 'uploadfile_ant',
                        formData: {
                            sbtype: sbtype,
                            filetype: 'sb',
                            orderNo: that.data.orderNo
                        },
                        header: {
                            "Content-Type": "multipart/form-data"
                        },
                        success: function (res) {
                            uploadImgCount++;
                            console.log(res);
                            if (sbtype == 2) {
                                var data = JSON.parse(res.data);
                                data = JSON.parse(data[0].piccontent);
                                var items = data.result.cardsinfo[0].items;
                                console.log(data.result.cardsinfo[0].items)
                                that.setData({
                                    ocrName: items[1].content,
                                    ocrSex: items[2].content,
                                    ocrNation: items[3].content,
                                    ocrBirth: items[4].content,
                                    ocrAddress: items[5].content,
                                    ocrIdNumber: items[6].content,
                                })
                                console.log("sbtype == 2:" + JSON.stringify(items))
                            } else if (sbtype == 3) {
                                var data = JSON.parse(res.data);
                                data = JSON.parse(data[0].piccontent);
                                var items = data.result.cardsinfo[0].items;
                                that.setData({
                                    ocrOffice: items[1].content,
                                    ocrValidityPeriod: items[2].content,
                                    cusIdValidfrom: items[3].content,
                                    cusIdValidto: items[4].content
                                })
                                console.log("sbtype == 3:" + JSON.stringify(items))

                            } else if (sbtype == 6) {
                                console.log(data.result.cardsinfo[0].items)
                                var data = JSON.parse(res.data);
                                data = JSON.parse(data[0].piccontent);
                                var items = data.result.cardsinfo[0].items;
                                that.setData({
                                    brand: items[5].content,
                                    plateNumber: items[1].content,
                                    holder: items[3].content,
                                    dateIssue: items[8].content,
                                    nature: items[10].content,
                                    address: items[4].content,
                                    vinno: items[6].content,
                                    engineNumber: items[7].content,
                                })
                                console.log("sbtype == 6:" + JSON.stringify(items))
                            } else if (sbtype == 5) {
                                var data = JSON.parse(res.data);
                                data = JSON.parse(data[0].piccontent);
                                var items = data.result.cardsinfo[0].items;
                                that.setData({
                                    holder: items[2].content,
                                    dateIssue: items[6].content,
                                    nature: items[7].content,
                                    address: items[4].content,

                                })
                                console.log(data);
                                console.log(items);
                            }
                            if (uploadImgCount == tempFilePaths.length) {
                                wx.hideToast();
                            }
                        },
                        fail: function (res) {
                            wx.hideToast();
                            wx.showModal({
                                title: '错误提示',
                                content: '上传图片失败',
                                showCancel: false
                            })
                        }
                    });
                }
            }
        });
    },
    submitBtn: function () {
        var that = this;
        var name = that.data.name;
        var accountNo = that.data.accountNo;
        var mobile = that.data.mobile;
        var bankCode = that.data.bankCode;
        var IdCard = that.data.IdCard;
        if (name ==" "|| name == null ){
            wx.showModal({
                title: '提示',
                content: '请填写姓名',
                showCancel: false,
            })
            return false;
        }
        if (IdCard == " " || IdCard == null) {
            wx.showModal({
                title: '提示',
                content: '请填写身份证号',
                showCancel: false,
            })
            return false;
        }
        if (accountNo == " " || accountNo == null) {
            wx.showModal({
                title: '提示',
                content: '请填写银行卡号',
                showCancel: false,
            })
            return false;
        }
        if (bankCode == " " || bankCode == null) {
            wx.showModal({
                title: '提示',
                content: '请填写银行代码',
                showCancel: false,
            })
            return false;
        } 
        if (mobile == " " || mobile == null) {
            wx.showModal({
                title: '提示',
                content: '请填写电话',
                showCancel: false,
            })
            return false;
        }
        
        wx.request({
            url: pathUrl + '/rest/fk/dotdqy',
            data: {
                realName: that.data.name,
                cardNo: that.data.IdCard,
                bankCardNo: that.data.accountNo,
                mobile: that.data.mobile,

            },
            method: 'POST',
            success: function (res) {
                console.log(res)
                
                if (res.data.ok) {
                    wx.showModal({
                        title: '提示',
                        content: res.data.data.rmContent,
                        confirmColor: '#7aa6d1',
                        showCancel: false
                    })
                    // if(res.data.data.rmContent == "已发送验证码"){
                    //     wx.request({
                    //         url: pathUrl + '/',
                    //     })
                    // }
                   
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        confirmColor: '#7aa6d1',
                        showCancel: false
                    })
                }
            }
        }) 
    },
    




    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        var openId = wx.getStorageSync('openId');
        this.setData({
            openId: openId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})